local extension = Package("rfenghou_qi")
extension.extensionName = "aaa_fenghou"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["rfenghou_qi"] = "哄齐",
}

local function AddWinAudio(general)
  local Win = fk.CreateActiveSkill{ name = general.name.."_win_audio" }
  Win.package = extension
  Fk:addSkill(Win)
end

local gaochanggong = General(extension, "rfenghou__gaochanggong", "qi", 4)
local ruzhen = fk.CreateTriggerSkill{
  name = "rfenghou__ruzhen",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.TurnEnd, fk.RoundStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.RoundStart then
        return true
      else
        return target == player and player:getMark(self.name) ~= 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.RoundStart then
      room:notifySkillInvoked(player, self.name, "drawcard")
      local cards = player:drawCards(3, self.name, "top", "@@rfenghou__ruzhen-inhand")
      room:setPlayerMark(player, self.name, table.simpleClone(cards))
    elseif event == fk.TurnStart then
      room:notifySkillInvoked(player, self.name, "negative")
      local cards = table.filter(player:getCardIds("h"), function (id)
        return table.contains(player:getTableMark(self.name), id)
      end)
      if #cards > 0 then
        player:showCards(cards)
      end
      if player.dead or player:isNude() then return end
      local n = #player:getTableMark(self.name) - #cards
      if n > 0 then
        room:askForDiscard(player, n, n, true, self.name, false)
      end
    elseif event == fk.TurnEnd then
      room:notifySkillInvoked(player, self.name, "negative")
      local cards = table.filter(player:getCardIds("h"), function (id)
        return table.contains(player:getTableMark(self.name), id)
      end)
      local n = #player:getTableMark(self.name) - #cards
      cards = table.filter(cards, function (id)
        return not player:prohibitDiscard(id)
      end)
      if #cards > 0 then
        room:throwCard(cards, self.name, player, player)
      end
      if player.dead or n < 1 then return end
      player:drawCards(n, self.name)
    end
  end
}
gaochanggong:addSkill(ruzhen)
Fk:loadTranslationTable{
  ["rfenghou__gaochanggong"] = "高长恭",
  ["#rfenghou__gaochanggong"] = "赤透假面",
  ["illustrator:rfenghou__gaochanggong"] = "珊瑚虫",
  ["designer:rfenghou__gaochanggong"] = "ab",
  ["cv:rfenghou__gaochanggong"] = "折翼",

  ["rfenghou__ruzhen"] = "入阵",
  [":rfenghou__ruzhen"] = "锁定技，轮次开始时，你摸三张牌；回合开始时，你展示这些牌，每少一张，你需弃置一张牌；回合结束时，你弃置这些牌，每少弃置一张牌，你摸一张牌。",
  ["@@rfenghou__ruzhen-inhand"] = "入阵",

  ["$rfenghou__ruzhen1"] = "（鼓声）",
  ["$rfenghou__ruzhen2"] = "家事亲切，不觉遂危。",
  ["$rfenghou__ruzhen3"] = "兰陵亲至，贼虏得破，且速与援。",
  ["~rfenghou__gaochanggong"] = "天颜，何由得见！",
}

local qihuangong = General(extension, "rfenghou__jiangxiaobai", "qi", 4)

local rfenghou__rangyi = fk.CreateTriggerSkill{
  name = "rfenghou__rangyi",
  events = {fk.CardUsing},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and (data.card.name == "jink" or data.card.name == "nullification") then
      return table.find(player.room.alive_players, function (p)
        return p ~= target and p:getHandcardNum() > 1 and not table.contains(player:getTableMark(self.name), p.id)
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p ~= target and p:getHandcardNum() > 1 and not table.contains(player:getTableMark(self.name), p.id)
    end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#rfenghou__rangyi-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:addTableMark(player, self.name, to.id)
    local num = to:getHandcardNum() // 2
    local cards = room:askForCardsChosen(player, to, num, num, "h", self.name)
    room:throwCard(cards, self.name, to, player)
    if not player.dead and table.every(room.alive_players, function (p)
      return table.contains(player:getTableMark(self.name), p.id)
    end) then
      room:setPlayerMark(player, self.name, 0)
    end
  end,
}
qihuangong:addSkill(rfenghou__rangyi)

local rfenghou__hemeng = fk.CreateTriggerSkill{
  name = "rfenghou__hemeng",
  events = {fk.CardUsing},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and ((data.card.multiple_targets and data.card.type == Card.TypeTrick)
    or (data.card.name == "fire__slash" or data.card.name == "thunder__slash" or data.card.name == "ice__slash"))
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room.alive_players, function (p)
      return not target:isProhibited(p, data.card)
      and data.card.skill:modTargetFilter(p.id, {}, data.from, data.card, true)
    end), Util.IdMapper)
    if #targets == 0 then return false end
    local num = math.min(#room.alive_players // 2, #targets)
    local prompt = "#rfenghou__hemeng-"..(target ~= player and "mustself" or "choose")..":::"..data.card:toLogString()..":"..num
    local _, dat = room:askForUseActiveSkill(player, "rfenghou__hemeng_active", prompt, true,
    {targets = targets, target_num = num, mustChooseSelf = target ~= player})
    if dat then
      room:sortPlayersByAction(dat.targets)
      self.cost_data = {tos = dat.targets}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.tos = table.map(self.cost_data.tos, function (id) return {id} end)
    room:sendLog{type = "#ChangeTarget", from = target.id, arg = data.card:toLogString(), to = self.cost_data.tos}
  end,
}
qihuangong:addSkill(rfenghou__hemeng)

local rfenghou__hemeng_active = fk.CreateActiveSkill{
  name = "rfenghou__hemeng_active",
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return table.contains(self.targets or {}, to_select) and #selected < self.target_num
  end,
  feasible = function(self, selected)
    if self.mustChooseSelf and not table.contains(selected, Self.id) then return false end
    return #selected == self.target_num
  end,
}
Fk:addSkill(rfenghou__hemeng_active)

local rfenghou__muxian = fk.CreateTriggerSkill{
  name = "rfenghou__muxian$",
  events = {fk.Damage},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target and not target.dead and data.to == player
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    self.cost_data = {tos = {target.id}}
    if target.kingdom == "qi" then
      return room:askForSkillInvoke(target, self.name, nil, "#rfenghou__muxian-recover:"..player.id)
    else
      return room:askForSkillInvoke(player, self.name, nil, "#rfenghou__muxian-draw:"..target.id)
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if target.kingdom == "qi" then
      room:recover { num = 1, skillName = self.name, who = player, recoverBy = target }
    else
      player:drawCards(1, self.name)
      if not target.dead then
        target:drawCards(1, self.name)
      end
      room:changeKingdom(target, "qi", true)
    end
  end,
}
qihuangong:addSkill(rfenghou__muxian)
AddWinAudio(qihuangong)
Fk:loadTranslationTable{
  ["rfenghou__jiangxiaobai"] = "姜小白", -- 齐桓公
  ["#rfenghou__jiangxiaobai"] = "一匡天下",
  ["illustrator:rfenghou__jiangxiaobai"] = "啪啪三国",
  ["designer:rfenghou__jiangxiaobai"] = "扬林",
  ["cv:rfenghou__jiangxiaobai"] = "丫奶",

  ["rfenghou__rangyi"] = "攘夷",
  [":rfenghou__rangyi"] = "一名角色使用【闪】或【无懈可击】时，你可以弃置未以此法弃置过的另一名角色半数手牌（向下取整）；你以此法弃置过所有存活角色的牌后，重置此技能。",
  ["#rfenghou__rangyi-choose"] = "攘夷：你可以弃置一名角色一半的手牌！",

  ["rfenghou__hemeng"] = "合盟",
  [":rfenghou__hemeng"] = "一名角色使用属性【杀】或多目标锦囊牌时，你可以将此牌目标改为场上半数由你指定的角色（向下取整，不足则全选）；若使用牌的角色不为你，你需为目标之一。",
  ["#rfenghou__hemeng-choose"] = "合盟：你可重新选择 %arg2 名角色作为%arg的目标",
  ["#rfenghou__hemeng-mustself"] = "合盟：你可重新选择 %arg2 名角色作为%arg的目标（须包含你！）",
  ["rfenghou__hemeng_active"] = "合盟",
  ["#ChangeTarget"] = "%from 使用的 %arg 目标被改为 %to",

  ["rfenghou__muxian"] = "募贤",
  [":rfenghou__muxian"] = "主公技，一名角色对你造成伤害后，若其为齐势力，其可以令你回复一点体力；若为其他势力，你可以与其各摸一张牌，再将其改为齐势力。",
  ["#rfenghou__muxian-recover"] = "募贤:你可以令 %src 回复一点体力",
  ["#rfenghou__muxian-draw"] = "募贤:你可以与 %src 各摸一张牌，再将其改为齐势力",

  ["$rfenghou__rangyi1"] = "受先君专征之任，内修政治，外攘夷狄。",
  ["$rfenghou__rangyi2"] = "尔贡包茅不入，王祭无以缩酒，寡人是征。",
  ["$rfenghou__hemeng1"] = "北伐山戎、南至召陵；九合诸侯，一匡天下！",
  ["$rfenghou__hemeng2"] = "凡我同盟之人，既盟之后，言归于好。",
  ["$rfenghou__muxian1"] = "布衣之士可轻爵禄，万乘之主岂可轻贤士？",
  ["$rfenghou__muxian2"] = "君者劳于索人，寡人得仲父，何为不易？",
  ["~rfenghou__jiangxiaobai"] = "仲父之谋，百不失一；寡人不听，是有今日……",
  ["$rfenghou__jiangxiaobai_win_audio"] = "既当一人，身有何劳？垂衣裳而天下定。",
}

return extension
